from socket import *
import threading

serverPort = 12000#端口号
serverSocket = socket(AF_INET, SOCK_STREAM)#创建套接字
serverSocket.bind(('localhost', serverPort))#将套接字与端口号绑定起来
serverSocket.listen(5)#表示可以同时接收5个套接字的信息
#套接字连接时函数
def connect():
    print(threading.current_thread().getName(), " Wait...")# 等待套接字连接
    connectSocket, adress = serverSocket.accept()#获取连接套接字和地址
    while True:
        message = connectSocket.recv(1024).decode()#接收的信息
        print(threading.current_thread().getName()," Accepted", adress, ":", message)#输出接收的信息
        connectSocket.send(message.encode())#返回接收的信息

# 多线程运行connect函数
threads = []
for i in range(5):
    threads.append(threading.Thread(target=connect))
    threads[i].start()#开启线程